using System.Runtime.InteropServices;
using System.ComponentModel;
using System;    
using System.ComponentModel.Design;
using Microsoft.Win32;
using System.Drawing;    
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.ComponentModel;
using System.Windows.Forms.Design;

namespace Microsoft.Samples
{
	/// <summary>
	/// Summary description for PercentEditor.
	/// </summary>
	
	public class PercentEditor : UITypeEditor 
	{
		private PercentUI percentUI;

		public override object EditValue(ITypeDescriptorContext context,  IServiceProvider  provider, object value) 
		{
			object returnValue = value;
			if (provider != null) 
			{
				IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
				if (edSvc != null) 
				{
					if (percentUI == null) 
					{
						percentUI = new PercentUI(this);
					}
					percentUI.Start(edSvc, value);
					edSvc.DropDownControl(percentUI);
					value = percentUI.Value;
					percentUI.End();
				}
			}
			return value;
		}

		public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) 
		{
			return UITypeEditorEditStyle.DropDown;
		}

		#region Control used by UITypeEditor
		public class PercentUI : System.Windows.Forms.UserControl
		{
			private IWindowsFormsEditorService edSvc;
			private PercentEditor editor = null;
			private double oldPercent;
			private object value;

			public PercentUI(PercentEditor editor)
			{
				this.editor = editor;
				InitializeComponent();
				IntPtr p = this.trackBar1.Handle;
				this.trackBar1.Height = 16;
			}

			public object Value 
			{ 
				get 
				{
					return value;
				}
			}
            
			public void End() 
			{
				value = null;
				edSvc = null;
			}

			public void Start(IWindowsFormsEditorService edSvc, object value) 
			{
				this.edSvc = edSvc;
				this.value = value;
				this.oldPercent = (double)value;

				this.domainUpDown1.SelectedIndex = (int)((double)value * 100);
				this.trackBar1.Value = (int)((double)value * 100);
			}

			private void Teardown( bool save ) 
			{
				if (!save) 
				{
					value = oldPercent;
				}
				edSvc.CloseDropDown();
			}

			private void trackBar1_ValueChanged(object sender, System.EventArgs e)
			{
				this.domainUpDown1.SelectedIndex = 100 - this.trackBar1.Value;
				value = (float)this.trackBar1.Value / 100.0;
			}

			private void domainUpDown1_SelectedItemChanged(object sender, System.EventArgs e)
			{
				this.trackBar1.Value = 100 - this.domainUpDown1.SelectedIndex;
				value = (float)this.trackBar1.Value / 100.0;
			}

			#region Component designer generated code
			private System.Windows.Forms.TrackBar trackBar1;
			private System.Windows.Forms.DomainUpDown domainUpDown1;

			/// <summary> 
			/// Required method for Designer support - do not modify 
			/// the contents of this method with the code editor.
			/// </summary>
			private void InitializeComponent()
			{
				this.trackBar1 = new System.Windows.Forms.TrackBar();
				this.domainUpDown1 = new System.Windows.Forms.DomainUpDown();
				((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
				this.SuspendLayout();
				// 
				// trackBar1
				// 
				this.trackBar1.AutoSize = false;
				this.trackBar1.Maximum = 100;
				this.trackBar1.Name = "trackBar1";
				this.trackBar1.Location = new System.Drawing.Point(0, 2);
				this.trackBar1.Size = new System.Drawing.Size(128, 16);
				this.trackBar1.TabIndex = 0;
				this.trackBar1.TickFrequency = 5;
				this.trackBar1.SmallChange = 5;
				this.trackBar1.LargeChange = 5;
				this.trackBar1.ValueChanged += new System.EventHandler(this.trackBar1_ValueChanged);
				// 
				// domainUpDown1
				// 
				this.domainUpDown1.Items.Add("100%");
				this.domainUpDown1.Items.Add("99%");
				this.domainUpDown1.Items.Add("98%");
				this.domainUpDown1.Items.Add("97%");
				this.domainUpDown1.Items.Add("96%");
				this.domainUpDown1.Items.Add("95%");
				this.domainUpDown1.Items.Add("94%");
				this.domainUpDown1.Items.Add("93%");
				this.domainUpDown1.Items.Add("92%");
				this.domainUpDown1.Items.Add("91%");
				this.domainUpDown1.Items.Add("90%");
				this.domainUpDown1.Items.Add("89%");
				this.domainUpDown1.Items.Add("88%");
				this.domainUpDown1.Items.Add("87%");
				this.domainUpDown1.Items.Add("86%");
				this.domainUpDown1.Items.Add("85%");
				this.domainUpDown1.Items.Add("84%");
				this.domainUpDown1.Items.Add("83%");
				this.domainUpDown1.Items.Add("82%");
				this.domainUpDown1.Items.Add("81%");
				this.domainUpDown1.Items.Add("80%");
				this.domainUpDown1.Items.Add("79%");
				this.domainUpDown1.Items.Add("78%");
				this.domainUpDown1.Items.Add("77%");
				this.domainUpDown1.Items.Add("76%");
				this.domainUpDown1.Items.Add("75%");
				this.domainUpDown1.Items.Add("74%");
				this.domainUpDown1.Items.Add("73%");
				this.domainUpDown1.Items.Add("72%");
				this.domainUpDown1.Items.Add("71%");
				this.domainUpDown1.Items.Add("70%");
				this.domainUpDown1.Items.Add("69%");
				this.domainUpDown1.Items.Add("68%");
				this.domainUpDown1.Items.Add("67%");
				this.domainUpDown1.Items.Add("66%");
				this.domainUpDown1.Items.Add("65%");
				this.domainUpDown1.Items.Add("64%");
				this.domainUpDown1.Items.Add("63%");
				this.domainUpDown1.Items.Add("62%");
				this.domainUpDown1.Items.Add("61%");
				this.domainUpDown1.Items.Add("60%");
				this.domainUpDown1.Items.Add("59%");
				this.domainUpDown1.Items.Add("58%");
				this.domainUpDown1.Items.Add("57%");
				this.domainUpDown1.Items.Add("56%");
				this.domainUpDown1.Items.Add("55%");
				this.domainUpDown1.Items.Add("54%");
				this.domainUpDown1.Items.Add("53%");
				this.domainUpDown1.Items.Add("52%");
				this.domainUpDown1.Items.Add("51%");
				this.domainUpDown1.Items.Add("50%");
				this.domainUpDown1.Items.Add("49%");
				this.domainUpDown1.Items.Add("48%");
				this.domainUpDown1.Items.Add("47%");
				this.domainUpDown1.Items.Add("46%");
				this.domainUpDown1.Items.Add("45%");
				this.domainUpDown1.Items.Add("44%");
				this.domainUpDown1.Items.Add("43%");
				this.domainUpDown1.Items.Add("42%");
				this.domainUpDown1.Items.Add("41%");
				this.domainUpDown1.Items.Add("40%");
				this.domainUpDown1.Items.Add("39%");
				this.domainUpDown1.Items.Add("38%");
				this.domainUpDown1.Items.Add("37%");
				this.domainUpDown1.Items.Add("36%");
				this.domainUpDown1.Items.Add("35%");
				this.domainUpDown1.Items.Add("34%");
				this.domainUpDown1.Items.Add("33%");
				this.domainUpDown1.Items.Add("32%");
				this.domainUpDown1.Items.Add("31%");
				this.domainUpDown1.Items.Add("30%");
				this.domainUpDown1.Items.Add("29%");
				this.domainUpDown1.Items.Add("28%");
				this.domainUpDown1.Items.Add("27%");
				this.domainUpDown1.Items.Add("26%");
				this.domainUpDown1.Items.Add("25%");
				this.domainUpDown1.Items.Add("24%");
				this.domainUpDown1.Items.Add("23%");
				this.domainUpDown1.Items.Add("22%");
				this.domainUpDown1.Items.Add("21%");
				this.domainUpDown1.Items.Add("20%");
				this.domainUpDown1.Items.Add("19%");
				this.domainUpDown1.Items.Add("18%");
				this.domainUpDown1.Items.Add("17%");
				this.domainUpDown1.Items.Add("16%");
				this.domainUpDown1.Items.Add("15%");
				this.domainUpDown1.Items.Add("14%");
				this.domainUpDown1.Items.Add("13%");
				this.domainUpDown1.Items.Add("12%");
				this.domainUpDown1.Items.Add("11%");
				this.domainUpDown1.Items.Add("10%");
				this.domainUpDown1.Items.Add("9%");
				this.domainUpDown1.Items.Add("8%");
				this.domainUpDown1.Items.Add("7%");
				this.domainUpDown1.Items.Add("6%");
				this.domainUpDown1.Items.Add("5%");
				this.domainUpDown1.Items.Add("4%");
				this.domainUpDown1.Items.Add("3%");
				this.domainUpDown1.Items.Add("2%");
				this.domainUpDown1.Items.Add("1%");
				this.domainUpDown1.Items.Add("0%");
				this.domainUpDown1.Location = new System.Drawing.Point(128, 2);
				this.domainUpDown1.Name = "domainUpDown1";
				this.domainUpDown1.Size = new System.Drawing.Size(48, 20);
				this.domainUpDown1.TabIndex = 1;
				this.domainUpDown1.BorderStyle = BorderStyle.FixedSingle;
				this.domainUpDown1.ReadOnly = true;
				this.domainUpDown1.SelectedItemChanged += new System.EventHandler(this.domainUpDown1_SelectedItemChanged);
				// 
				// PercentUI
				// 
				this.Controls.AddRange(new System.Windows.Forms.Control[] {
																			  this.domainUpDown1,
																			  this.trackBar1});
				this.Name = "PercentUI";
				this.Size = new System.Drawing.Size(176, 24);
				((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
				this.ResumeLayout(false);

			}

			/// <summary> 
			/// Clean up any resources being used.
			/// </summary>
			protected override void Dispose( bool disposing )
			{
				base.Dispose( disposing );
			}
			#endregion
		}
		#endregion
	}
}
	

